package com.gzhryc.shared_device.oem.h5.mng.controllers.market;

import com.gzhryc.common.DateTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.core.enums.EGoodsClassify;
import com.gzhryc.net_marketing.core.enums.EReceiveGoodsType;
import com.gzhryc.net_marketing.core.enums.GoodsOrderEnum;
import com.gzhryc.net_marketing.supplier.pay.dao.db.SupplierBusinessOrder;
import com.gzhryc.net_marketing.supplier.pay.dao.db.SupplierBusinessOrderLog;
import com.gzhryc.net_marketing.supplier.pay.services.SupplierBusinessOrderLogService;
import com.gzhryc.net_marketing.supplier.pay.services.SupplierBusinessOrderService;
import com.gzhryc.net_marketing.supplier.pay.services.dto.SupplierBusinessOrderSearch;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

public class SupplierBusinessOrderController {

    @Route(value = { "/business/supplier/order/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", GoodsOrderEnum.State.values());
        request.setAttribute("supplyStateList", GoodsOrderEnum.SupplyState.values());
        return "/supplier/order/list.html";
    }

    @Route(value = "/business/supplier/order/list.data")
    public ListResult<SupplierBusinessOrder> listData(HttpServletRequest request) {
        ListResult<SupplierBusinessOrder> result = new ListResult<SupplierBusinessOrder>(DtoConstants.SUCCESS);

        SupplierBusinessOrderSearch search = WebUtils.getParameter(request, SupplierBusinessOrderSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(SupplierBusinessOrderService.self().countBySearch(search));
        result.getBody().setData(SupplierBusinessOrderService.self().findBySearch(search, pageNo, pageLimit));

        if (result.getBody().getTotal() == 0) {
            result.setCode(DtoConstants.NULL_DATA, "暂无数据");
        }

        return result;
    }

    @Route(value = { "/business/supplier/order/info.html" })
    public String info(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        SupplierBusinessOrder businessOrder = SupplierBusinessOrderService.self().getByTradeNo(tradeNo);
        if(businessOrder != null) {
            WebUtils.setAttribute(request, businessOrder);
        }
        return "/supplier/order/info.html";
    }

    @Route(value = { "/business/supplier/order/state/search.do" })
    public AjaxResult doSearch(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "查询订单支付状态失败");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        if(StringTools.isNotBlank(tradeNo)) {
            SupplierBusinessOrder order = SupplierBusinessOrderService.self().getByTradeNo(tradeNo);
            if (order == null) {
                result.setMsg("未找到订单信息");
                return result;
            }

            try {
                if(SupplierBusinessOrderService.self().searchOrderInfo(null,order)) {
                    result.setCode(DtoConstants.SUCCESS,null);
                }
            } catch (LogicException e) {
                result.setBody(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/business/supplier/order/confirm.do" })
    public AjaxResult doConfirm(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "确定订单操作失败");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        if(StringTools.isNotBlank(tradeNo)) {
            Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
            SupplierBusinessOrder order = SupplierBusinessOrderService.self().getByTradeNo(tradeNo);
            if (order == null) {
                result.setMsg("未找到订单信息");
                return result;
            }

            OperateInfo operateInfo = new OperateInfo();
            operateInfo.setProgramType(WebConstants.PROGRAM_TYPE);
            operateInfo.setOperatorType(EOperatorType.Manager.index());
            operateInfo.setOperator(manager.getFullName());

            try {
                if(SupplierBusinessOrderService.self().confirmOrder(order,GoodsOrderEnum.SupplyState.FinishConfirm.index(),operateInfo)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setBody(e.getMessage());
            }

        }
        return result;
    }

    /**
     * 发货
     * @param request
     * @return
     */
    @Route(value = { "/business/supplier/order/deliver.html" })
    public String deliverGoods(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        SupplierBusinessOrder order = SupplierBusinessOrderService.self().getByTradeNo(tradeNo);
        if(order != null){
            WebUtils.setAttribute(request,order);
            if(EGoodsClassify.Fictitious.index().equals(order.getGoodsClassify())) {
                return "/business/order/fictitious.html";
            }
        }
        return "/supplier/order/logistics.html";
    }

    /**
     * 发货处理
     * @param request
     * @return
     */
    @Route(value = { "/business/supplier/order/deliver.do" })
    public AjaxResult doDeliverGoods(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"发货操作失败");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Date deliverGoodsDate = WebUtils.getDate(request,"deliverGoodsDate");
        if(StringTools.isNotBlank(tradeNo) && deliverGoodsDate != null) {
            Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
            SupplierBusinessOrder order = SupplierBusinessOrderService.self().getByTradeNo(tradeNo);
            if(order == null){
                result.setMsg("未找到订单信息");
                return result;
            }

            OperateInfo operateInfo = new OperateInfo();
            operateInfo.setProgramType(WebConstants.PROGRAM_TYPE);
            operateInfo.setOperatorType(EOperatorType.Manager.index());
            operateInfo.setOperator(manager.getFullName());

            if(EGoodsClassify.Fictitious.index().equals(order.getGoodsClassify())){
                String deliverGoodsNote = WebUtils.getString(request,"deliverGoodsNote");
                try {
                    if(SupplierBusinessOrderService.self().deliveryGoods(order,deliverGoodsNote,deliverGoodsDate,operateInfo)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                } catch (LogicException e) {
                    result.setBody(e.getMessage());
                }
            }else{
                String logisticsCompanyId = WebUtils.getString(request,"logisticsCompanyId");
                String logisticsTradeNo = WebUtils.getString(request,"logisticsTradeNo");
                try {
                    if(SupplierBusinessOrderService.self().logisticsDeliveryGoods(order,logisticsCompanyId,logisticsTradeNo,deliverGoodsDate,operateInfo)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                } catch (LogicException e) {
                    result.setBody(e.getMessage());
                }
            }
        }
        return result;
    }

    @Route(value = { "/business/supplier/order/receive.html" })
    public String receiveGoods(HttpServletRequest request) {
        return "/supplier/order/receive.html";
    }

    @Route(value = { "/business/supplier/order/receive.do" })
    public AjaxResult doReceiveGoods(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "确认收货失败");
        String tradeNo = WebUtils.getString(request, "tradeNo");
        Date receiveGoodsDate = WebUtils.getDate(request, "receiveGoodsDate");
        if (StringTools.isNotBlank(tradeNo) && receiveGoodsDate != null) {
            Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
            if (manager == null) {
                result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
                return result;
            }

            OperateInfo operateInfo = new OperateInfo();
            operateInfo.setProgramType(WebConstants.PROGRAM_TYPE);
            operateInfo.setOperatorType(EOperatorType.Manager.index());
            operateInfo.setOperator(manager.getFullName());

            try {
                if(SupplierBusinessOrderService.self().receiveGoods(tradeNo, EReceiveGoodsType.ManagerConfirm.index(),receiveGoodsDate,operateInfo)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/business/supplier/order/evaluate.html" })
    public String evaluateGoods(HttpServletRequest request) {
        return "/supplier/order/evaluate.html";
    }

    @Route(value = { "/business/supplier/order/evaluate.do" })
    public AjaxResult doEvaluateGoods(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "评价失败");
        String tradeNo = WebUtils.getString(request, "tradeNo");
        if (StringTools.isNotBlank(tradeNo)) {
            Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
            try {
                SupplierBusinessOrder businessOrder = SupplierBusinessOrderService.self().getByTradeNo(tradeNo);
                if(businessOrder == null){
                    throw new LogicException("未找到业务订单");
                }

                if(!GoodsOrderEnum.State.WaitEvaluate.index().equals(businessOrder.getState())){
                    throw new LogicException("业务订单未收货");
                }else{
                    Integer times = DateTools.differenceDay(businessOrder.getReceiveGoodsDate(),new Date());
                    if(times < 7){
                        throw new LogicException("业务订单收货未到7天");
                    }
                }

                OperateInfo operateInfo = new OperateInfo();
                operateInfo.setProgramType(WebConstants.PROGRAM_TYPE);
                operateInfo.setOperator(manager.getFullName());
                operateInfo.setOperatorType(EOperatorType.Manager.index());
                operateInfo.setOperateNote("默认评价");

                if (SupplierBusinessOrderService.self().evaluateGoods(null,businessOrder,operateInfo)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = "/business/supplier/order/logs/list.data")
    public ListResult<SupplierBusinessOrderLog> logsListData(HttpServletRequest request) {
        ListResult<SupplierBusinessOrderLog> result = new ListResult<SupplierBusinessOrderLog>(DtoConstants.NULL_DATA,"暂无数据");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        result.getBody().setData(SupplierBusinessOrderLogService.self().findByTradeNo(tradeNo));
        if(result.getBody().getData() != null && result.getBody().getData().size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }
}
